package net.techwatch.commons;

import net.techwatch.commons.backup.BackupTool;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author wilfried
 *
 */
public class DbToolFactory {

	private static final Log LOG = LogFactory.getLog(DbToolFactory.class);
	
	private static Options options = new Options();
	static {
		options.addOption("target", true, "Target that contains backup");
		options.addOption("table", true, "Database table name");
	}
	
	/**
	 * @param clArgs
	 * @return
	 */
	public static DbTool buildDbTool(String[] clArgs) {
		CommandLineParser parser = new PosixParser();
		try {
			CommandLine cmd = parser.parse(options, clArgs);
			
			// inititialize tool
			BackupTool tool = new BackupTool();
			tool.init();
			
			// fill tool arguments
			tool.setFilename(cmd.getOptionValue("target"));
			tool.setTablename(cmd.getOptionValue("table"));
			
			return tool;
		} catch(ParseException e) {
			LOG.fatal("Cannot parse arguments", e);
			return null;
		} catch(InitializationException e) {
			LOG.fatal("Initialization failed", e);
			return null;
		}
		
	}
	
	/**
	 * @return
	 */
	static int countOptions() {
		return options.getOptions().size();
	}
}
